<?php
namespace Common\Model;

use Think\Model;


class SlideModel extends Model
{

    public $_validate = array();


    public function addSlide()
    {
        $this->_validate = array(
            array('title', 'require', '幻灯片标题不能为空'),
        );
        if (!$this->create()) {
            return false;
        }
        $data = $_POST;
        $res = upload(null, false, 'slide', null);
        if ($res['code']) {
            $data['img'] = $res['path'];
            $this->add($data);
            return true;
        } else {
            $this->error = $res['path'];
            return false;
        }
    }

    public function getList(){
        $p = I('p',0,'int');
        $limit = C('DEFALUT_PAGE_SIZE');
        $startNum = $p > 0 ? ($p - 1) * $limit : 0;
        $count = $this->field("count(*) as count")->find();
        $data = $this->limit($startNum,$limit)->select();
        return array(
            'count' => $count['count'],
            'data'  => $data
        );
    }

    public function editSlide()
    {
        $this->_validate = array(
            array('title', 'require', '幻灯片标题不能为空'),
        );
        if (!$this->create()) {
            return false;
        }
        $data = $_POST;
        $data['id'] = I('get.id');
        $key = array_keys($_FILES)[0];
        if ($_FILES && $_FILES[$key]['error'] != 4) {
            $res = upload(null, true, 'slide', null, 88, 31);
            if ($res['code']) {
                $data['img'] = $res['path'];
            } else {
                $this->error = $res['path'];
                return false;
            }
        }
        $this->save($data);
        return true;
    }

    public function getSliderByType($type){
        $map['type'] = $type;
        $map['is_show'] = 1;
        return $this->where($map)->select();
    }
}
